﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using System.IO;
using System.Xml.Serialization;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace BWClient
{
    /// <summary>
    /// 序列化帮助
    /// </summary>
    public class SerializationHelper
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public SerializationHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 从XML文件加载到Info（反序列化）
        /// </summary>
        /// <param name="type"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static object Load(Type type, string filename)
        {
            FileStream oFileStream = null;
            try
            {
                // open the stream...
                oFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(type);
                return serializer.Deserialize(oFileStream);
            }
            catch
            {
                return null;
            }
            finally
            {
                if (oFileStream != null)
                    oFileStream.Close();
            }
        }

        /// <summary>
        /// 保存Info到XML（序列化）
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="filename">文件路径</param>
        public static bool Save(object obj, string filename)
        {
            bool success = false;

            FileStream fs = null;

            try
            {
                fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);
                success = true;
            }
            catch
            {
                success = false;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return success;

        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetString(Type type, object obj)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer ser = new XmlSerializer(type);
                ser.Serialize(sw, obj);
                return sw.ToString();
            }
        }
        /// <summary>
        /// 根据字符串获取实体
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetObj(string value, Type type)
        {
            using (StringReader sr = new StringReader(value))
            {
                XmlSerializer xz = new XmlSerializer(type);
                return xz.Deserialize(sr);
            }
        }
    }

}
